package stc.skymobi.transport.protocol.esb.hdr;

import java.util.ArrayList;
import java.util.Arrays;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import stc.skymobi.bean.esb.annotation.EsbField;
import stc.skymobi.bean.esb.core.TerminalBroadCastRule;


// in m2a header
//+-+-+-+-+-+-+-+
//|condCount(1) |
//+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ >-----+
//| condIndex(2)                |   condDataLen(2)                |       | 
//+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+       +->  数量为condCount
//|                         condData[condDataLen]......           |       |
//+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ >-----+

/**
 * E2A的广播消息适配规则
 * 如果是条件广播的内容 则需要和当前特定用户规则表匹配上才会下发广播内容
 * 
 *  @author Bluces.Wang@sky-mobi.com
 */

public class ETFTerminalBroadCastRule {
	public static final short SET_BRDCAST_COND_OPER_REPLACE = 0;
	public static final short SET_BRDCAST_COND_OPER_ADD = 1;
	
	@EsbField(index = 1 ,bytes = 1)
	private short condCount;
	
	@EsbField(index = 2, length="condCount" )
	private	ArrayList<Condition> conds;
	
	/**
	 * 规则条件
	 */
	public static class Condition{
		@EsbField(index = 3, bytes = 2,description="条件索引号")
		private int condIndex; 
		@EsbField(index = 4, bytes = 2)
		private int condDataLen;
		@EsbField(index = 5, length="condDataLen",customCodec=stc.skymobi.bean.bytebean.codec.ByteArrayCodec.class)
		private byte[] condData;
		@Override
		public String toString() {
			return "Condition [condIndex=" + condIndex + ", condData="+ Arrays.toString(condData) + "]";
		}
		public int getCondIndex() {
			return condIndex;
		}
		public void setCondIndex(int condIndex) {
			this.condIndex = condIndex;
		}
		public int getCondDataLen() {
			return condDataLen;
		}
		public void setCondDataLen(int condDataLen) {
			this.condDataLen = condDataLen;
		}
		public byte[] getCondData() {
			return condData;
		}
		public void setCondData(byte[] condData) {
			this.condData = condData;
		}
	}
    
    public String toString() {
        return  ToStringBuilder.reflectionToString(this, 
                            ToStringStyle.SHORT_PREFIX_STYLE);
    }

	public short getCondCount() {
		return condCount;
	}

	public void setCondCount(short condCount) {
		this.condCount = condCount;
	}

	public ArrayList<Condition> getConds() {
		return conds;
	}

	public void setConds(ArrayList<Condition> conds) {
		this.conds = conds;
	}

	public TerminalBroadCastRule toTerminalBroadCastRule() {
		TerminalBroadCastRule ret = new TerminalBroadCastRule();
		ret.setCondCount(this.getCondCount());
		ArrayList<TerminalBroadCastRule.Condition> conditions = new ArrayList<TerminalBroadCastRule.Condition>();
		for (Condition cond : this.getConds()) {
			TerminalBroadCastRule.Condition tmp = new TerminalBroadCastRule.Condition();
			tmp.setCondIndex(cond.getCondIndex());
			tmp.setCondData(cond.getCondData());
			conditions.add(tmp);
		}
		ret.setConds(conditions);
		return	ret;
	}
}
